/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 KylinSoft  Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#ifndef SHORTCUTUI_H
#define SHORTCUTUI_H

#include "settinggroup.h"
#include "addbutton.h"
#include "doubleclicklineedit.h"
#include "clickfixlabel.h"
#include "addshortcutdialog.h"

#include <QHBoxLayout>
#include <QWidget>
#include <QMenu>
#include <QMessageBox>
#include <QGSettings>
#include <klabel.h>

using namespace kdk;

class ShortcutUi : public QWidget
{
    Q_OBJECT
public:
    ShortcutUi();
    ~ShortcutUi();
    void addSystemShortcut(const QString &name, const QString &shortcut, const QString &mediaKey, KeyEntryList *systemEntryList, KeyEntryList *windowEntryList, KeyEntryList *customEntryList);
    void addWindowShortcut(const QString &name, const QString &shortcut, const QString &mediaKey, KeyEntryList *systemEntryList, KeyEntryList *windowEntryList, KeyEntryList *customEntryList);
    void addCustomShortcut(KeyEntry keyEntry, KeyEntryList *systemEntryList, KeyEntryList *windowEntryList, KeyEntryList *customEntryList);
    void clearCustomShorcutFrame();
    void setWindowValid(bool isValid);
    void setWindowHide();
    void addDefaultBtn();
    void loadDefaultShortcut();

private:
    void initUi();
    void initSystem();
    void initWindow();
    void initCustom();
    void updateGeneralShort(DoubleClickShortCut *shortCutEdit, QString key, QString value, bool isConflict);

    QString getShowShortcutString(QString src);
    QString keyToLib(QString key);
    QString conflictTip(const QString &key, const QString &value);
    void defaultButtonSlot();
    void setWindowShortcutToNull();
    void defaultShortcut();
    void setCustomShortcutToNull();
    QString keyToSequence(QString key);
    void labelDoubleClickSlot(ClickFixLabel* fixLabel,
                              DoubleClickLineEdit* lineEdit);
    void labelDoubleClickSlot(ClickFixLabel* fixLabel,
                              DoubleClickShortCut *lineEdit);
    void lineEditFocusOutSlot(DoubleClickShortCut* lineEdit,
                              ClickFixLabel *fixLabel);
    void lineEditFocusOutSlot(DoubleClickLineEdit* lineEdit,
                              ClickFixLabel *fixLabel);
    void lineEditChangeSlot(DoubleClickShortCut* lineEdit,
                            ClickFixLabel *fixLabel,
                            QString key);
    void customLineEditStrChangeSlot(DoubleClickLineEdit* lineEdit,
                                     ClickFixLabel *fixLabel,
                                     KeyEntry* keyEntry);
    void customLineEditChangeSlot(DoubleClickShortCut* lineEdit,
                                  ClickFixLabel *fixLabel,
                                  KeyEntry* keyEntry);
    void lineEditConflictSlot(DoubleClickShortCut* lineEdit,
                              ClickFixLabel *fixLabel,
                              QString key);
    void customLineEditConflictSlot(DoubleClickShortCut* lineEdit,
                                    ClickFixLabel *fixLabel,
                                    KeyEntry* keyEntry);
    void lineEditOccupySlot(DoubleClickShortCut* lineEdit,
                            ClickFixLabel* fixLabel);
    void customAddDialogSlot(AddShortcutDialog *addDialog,
                             DoubleClickLineEdit *lineEdit,
                             DoubleClickShortCut *bingdLineEdit,
                             ClickFixLabel* fixLabeL,
                             ClickFixLabel* bindFixLabel,
                             KeyEntry* keyEntry,
                             QString name, QString exec, QString key);
private:
    QVBoxLayout *uiLayout     = nullptr;
    QVBoxLayout *systemLayout = nullptr;
    QVBoxLayout *customLayout = nullptr;
    QVBoxLayout *m_windowLayout = nullptr;
    QHBoxLayout *m_defaultLayout = nullptr;

private:
    KLabel   *systemTitle         = nullptr;
    SettingGroup *systemShortcutGroup = nullptr;

    KLabel   *m_windowTitle         = nullptr;
    SettingGroup *m_windowShortcutGroup = nullptr;
    QPushButton* m_defaultButton = nullptr;

private:
    KLabel   *customTitle         = nullptr;
    SettingGroup *customShortcutGroup = nullptr;
    AddButton    *addButton           = nullptr;

    QList<KeyEntry>              mSystemEntryList;
    QList<KeyEntry>              mCustomEntryList;
    QList<KeyEntry>              m_windowEntryList;

    QList<DoubleClickShortCut *> mGeneralLineEdit;
    QList<ClickFixLabel *>       mGeneralName;
    QList<DoubleClickShortCut *> mCustomLineEdit;
    QList<ClickFixLabel *>       mCustomName;
    QList<DoubleClickShortCut *> m_windowLineEdit;
    QList<ClickFixLabel *>       m_windowName;

    QMap<QString, QString>       mAllKeyI18nName;
    bool m_windowShortcutValid = true;
    QDBusInterface* m_shortcutInterface = nullptr;
    QList<QString> m_defaultShortcutList;

Q_SIGNALS:
    void deleteShortcut(const QString &path);
    void toCreateShortcut(QString path, QString name, QString exec, QString key, bool buildFlag = true, bool convertFlag = true, DoubleClickShortCut *shortCut = nullptr);
    void addButtonClicked();
    void updateSystemShortcut(const QString &key, const QString &value);
    void updateCustomShortcut(const QByteArray &path, const QString &key, const QString &value);
};

#endif // SHORTCUTUI_H
